package com.google.code.irouter.portal.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.appengine.api.datastore.KeyFactory;
import com.google.code.irouter.batch.api.Detector;
import com.google.code.irouter.batch.api.Sender;
import com.google.code.irouter.model.Api;
import com.google.code.irouter.model.Model;
import com.google.code.irouter.portal.client.rpc.api.ApiResult;
import com.google.code.irouter.portal.client.rpc.api.ApiService;
import com.google.code.irouter.utils.ApiUtil;
import com.google.code.irouter.utils.DateTimeUtil;
import com.google.code.irouter.utils.ModelUtil;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ApiServiceImpl extends RemoteServiceServlet implements ApiService {

	private static final long serialVersionUID = 3161751227456051456L;

	@Override
	public List<ApiResult> loadDetector() throws IllegalArgumentException {
		return list(Detector.class.getSimpleName());
	}
	
	@Override
	public List<ApiResult> loadSender() throws IllegalArgumentException {
		return list(Sender.class.getSimpleName());
	}
	
	private List<ApiResult> list(String type) throws IllegalArgumentException {
		List<ApiResult> result = new ArrayList<ApiResult>();
		Map<String, String> apiMap = ApiUtil.list(type);
		Set<String> keySet = apiMap.keySet();
		for(String key : keySet) {
			ApiResult apiResult = new ApiResult();
			apiResult.setId(key);
			apiResult.setName(apiMap.get(key));
			result.add(apiResult);
		}
		return result;
	}

	@Override
	public List<ApiResult> load(String type) throws IllegalArgumentException {
		List<ApiResult> result = null;
		try {
			Map<String, Object> query = new HashMap<String, Object>();
			query.put("type", type);
			List<Model> modelList = ModelUtil.select(Api.class, query);
			result = new ArrayList<ApiResult>();
			if(modelList.size() > 0) {
				for(Model model : modelList) {
					Api api = (Api) model;
					ApiResult aprResult = new ApiResult();
					aprResult.setId(String.valueOf(api.getKey().getId()));
					aprResult.setName(api.getName());
					aprResult.setType(api.getType());
					aprResult.setRefrence(api.getRefrence());
					aprResult.setCreateDate(DateTimeUtil.formatDate(api.getCreateDate()));
					aprResult.setUpdateDate(DateTimeUtil.formatDate(api.getUpdateDate()));
					result.add(aprResult);					
				}
			}
		} catch (Exception exception) {
			// TODO
		}
		return result;
	}

	@Override
	public boolean remove(String[] ids) throws IllegalArgumentException {
		boolean result = true;
		try {
			for(String id : ids) {
				ApiUtil.remove(id);
			}			
		} catch (Exception exception) {
			// TODO
			result = false;
		}
		return result;
	}

	@Override
	public boolean save(String id, String name, String type, String refrence)
			throws IllegalArgumentException {
		boolean result = true;
		try {
			if(id.trim().length() == 0) {
				ApiUtil.add(
						type, 
						name, 
						refrence
				);
			} else {
				ApiUtil.update(id, name, type, refrence);
			}
		} catch (Exception exception) {
			// TODO
			result = false;
		}
		return result;
	}

	@Override
	public ApiResult get(String id) throws IllegalArgumentException {
		List<Model> modelList = ModelUtil.select(Api.class, KeyFactory.createKey(Api.class.getSimpleName(), Long.valueOf(id)));
		ApiResult apiResult = null;
		if(modelList.size() > 0) {
			apiResult = new ApiResult();
			Api api = (Api) modelList.get(0);
			apiResult.setId(id);
			apiResult.setName(api.getName());
			apiResult.setType(api.getType());
			apiResult.setRefrence(api.getRefrence());
		}
		return apiResult;
	}
}
